package com.demo.controller;

import com.demo.domain.User;
import com.demo.exception.BusinessException;
import com.demo.exception.BaseException;
import com.demo.exception.ExceptionMsgConfig;
import com.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

//

/**
 * RestController 返回json格式的rest，包含了@Controller @ResponseBody
 */
@Controller
public class DemoController {

    @Autowired
    private IUserService userService;
    @Autowired
    ExceptionMsgConfig exceptionMsgConfig;

    @RequestMapping("/index")
    @ResponseBody
    public User index(Integer id, String name) throws BaseException {

        if (StringUtils.isEmpty(id)) {
            throw new BusinessException(1002,new String[]{"id", "test1"});
        }
        //测试异常
        int count = 10/id;
        return userService.findUserById(id);
    }

    @RequestMapping("/ftl")
    public String freemarkerDemo(Map<String, User> map) {
        User user = new User();
        user.setId(1201);
        user.setName("jim");
        map.put("user", user);
        return "freemarkerDemo";
    }
}
